home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 2 of 3 / CHAPTER3 / GETBTN.C < prev    next >
C/C++ Source or Header  |  1996-03-06  |  9KB  |  256 lines

  1.  
  2. #include <windows.h>  
  3. #include <commctrl.h>
  4. #include "getbtn.h"  
  5.  
  6.  
  7.  
  8. #if defined (WIN32)
  9.     #define IS_WIN32 TRUE
  10. #else
  11.     #define IS_WIN32 FALSE
  12. #endif
  13.  
  14. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  15. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  16. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  17.  
  18. HINSTANCE hInst;   // current instance
  19.  
  20. LPCTSTR lpszAppName = "MyApp";
  21. LPCTSTR lpszTitle   = "TB_GETBUTTON"; 
  22.  
  23.  
  24. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  25.  
  26.  
  27. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  28.                       LPTSTR lpCmdLine, int nCmdShow)
  29. {
  30.    MSG      msg;
  31.    HWND     hWnd; 
  32.    WNDCLASS wc;
  33.  
  34.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  35.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  36.    wc.cbClsExtra    = 0;                      
  37.    wc.cbWndExtra    = 0;                      
  38.    wc.hInstance     = hInstance;              
  39.    wc.hIcon         = LoadIcon (hInstance, lpszAppName); 
  40.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  41.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  42.    wc.lpszMenuName  = lpszAppName;              
  43.    wc.lpszClassName = lpszAppName;              
  44.  
  45.    if ( IS_WIN95 )
  46.    {
  47.       if ( !RegisterWin95( &wc ) )
  48.          return( FALSE );
  49.    }
  50.    else if ( !RegisterClass( &wc ) )
  51.       return( FALSE );
  52.  
  53.    hInst = hInstance; 
  54.  
  55.    hWnd = CreateWindow( lpszAppName, 
  56.                         lpszTitle,    
  57.                         WS_OVERLAPPEDWINDOW, 
  58.                         CW_USEDEFAULT, 0, 
  59.                         CW_USEDEFAULT, 0,  
  60.                         NULL,              
  61.                         NULL,              
  62.                         hInstance,         
  63.                         NULL               
  64.                       );
  65.  
  66.    if ( !hWnd ) 
  67.       return( FALSE );
  68.  
  69.    ShowWindow( hWnd, nCmdShow ); 
  70.    UpdateWindow( hWnd );         
  71.  
  72.    while( GetMessage( &msg, NULL, 0, 0) )   
  73.    {
  74.       TranslateMessage( &msg ); 
  75.       DispatchMessage( &msg );  
  76.    }
  77.  
  78.    return( msg.wParam ); 
  79. }
  80.  
  81.  
  82. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  83. {
  84.    WNDCLASSEX wcex;
  85.  
  86.    wcex.style         = lpwc->style;
  87.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  88.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  89.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  90.    wcex.hInstance     = lpwc->hInstance;
  91.    wcex.hIcon         = lpwc->hIcon;
  92.    wcex.hCursor       = lpwc->hCursor;
  93.    wcex.hbrBackground = lpwc->hbrBackground;
  94.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  95.    wcex.lpszClassName = lpwc->lpszClassName;
  96.  
  97.    // Added elements for Windows 95.
  98.    //...............................
  99.    wcex.cbSize = sizeof(WNDCLASSEX);
  100.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  101.                             IMAGE_ICON, 16, 16,
  102.                             LR_DEFAULTCOLOR );
  103.             
  104.    return RegisterClassEx( &wcex );
  105. }
  106.  
  107. // Toolbar buttons. 
  108. //.................
  109. TBBUTTON tbButtons[] = 
  110.     { STD_FILENEW,  IDM_NEW, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0L, 0}, 
  111.     { STD_FILEOPEN, IDM_OPEN, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0L, 0}, 
  112.     { STD_FILESAVE, IDM_SAVE, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0L, 0}, 
  113.     { 0, 0, TBSTATE_ENABLED, TBSTYLE_SEP, 0, 0L, 0}, 
  114.     { VIEW_LARGEICONS, IDM_LARGEICON, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0L, 0}, 
  115.     { VIEW_SMALLICONS, IDM_SMALLICON, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0L, 0}, 
  116.     { VIEW_LIST, IDM_LISTVIEW, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0L, 0}, 
  117.     { VIEW_DETAILS, IDM_REPORTVIEW, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0L, 0}, 
  118. }; 
  119.  
  120.  
  121. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  122. {
  123. static HWND hToolWnd   = NULL;
  124.  
  125.    switch( uMsg )
  126.    {
  127.       case WM_CREATE :
  128.               // Initialize the common control library
  129.               // and create the status window and toolbar.
  130.               //..........................................
  131.               InitCommonControls();
  132.  
  133.               hToolWnd = CreateToolbarEx( hWnd, 
  134.                                           WS_CHILD | WS_BORDER | WS_VISIBLE, 
  135.                                           102, 
  136.                                           11,
  137.                                           (HINSTANCE)HINST_COMMCTRL, 
  138.                                           IDB_STD_SMALL_COLOR, 
  139.                                           tbButtons, 4,
  140.                                           0, 0, 100, 30, sizeof(TBBUTTON) );
  141.  
  142.               if ( hToolWnd )
  143.               {
  144.                  TBADDBITMAP tb;
  145.                  int index, stdidx;
  146.  
  147.                  // Add the system-defined view bitmaps.
  148.                  //.....................................
  149.                  tb.hInst = HINST_COMMCTRL;
  150.                  tb.nID = IDB_VIEW_SMALL_COLOR;
  151.                  stdidx = SendMessage(hToolWnd, TB_ADDBITMAP, 12, (LPARAM)&tb);
  152.  
  153.                  for (index = 4; index < 8; index++)
  154.                     tbButtons[index].iBitmap += stdidx;
  155.  
  156.                  SendMessage(hToolWnd, TB_ADDBUTTONS, 4, (LONG) &tbButtons[4]);
  157.               }
  158.               break;
  159.  
  160.  
  161.       case WM_COMMAND :
  162.               switch( LOWORD( wParam ) )
  163.               {
  164.                  case IDM_NEW :
  165.                  case IDM_OPEN :
  166.                  case IDM_SAVE :
  167.                  case IDM_LARGEICON :
  168.                  case IDM_SMALLICON :
  169.                  case IDM_LISTVIEW :
  170.                  case IDM_REPORTVIEW :
  171.                         {
  172.                            TBBUTTON tb;
  173.                            RECT     rect;
  174.                            HDC      hdc;
  175.                            char     szTmp[64];
  176.  
  177.                            // Get the index of the button.
  178.                            //.............................
  179.                            int nIdx = SendMessage(hToolWnd, TB_COMMANDTOINDEX, 
  180.                                                   LOWORD( wParam ), 0 );
  181.  
  182.                            // Get the button information. 
  183.                            //............................
  184.                            SendMessage(hToolWnd, TB_GETBUTTON, 
  185.                                        nIdx, (LPARAM)&tb );
  186.  
  187.                            // Get the rect of the button.
  188.                            //............................
  189.                            SendMessage(hToolWnd, TB_GETITEMRECT,
  190.                                        nIdx, (LPARAM)&rect );
  191.  
  192.                            // Output button information.
  193.                            //...........................
  194.                            hdc = GetDC( hWnd );
  195.  
  196.                            wsprintf(szTmp, "Button index: %d", tb.iBitmap );
  197.                            TextOut( hdc, 10, 40, szTmp, strlen( szTmp ) );
  198.  
  199.                            wsprintf(szTmp, "Button command: %d", tb.idCommand );
  200.                            TextOut( hdc, 10, 60, szTmp, strlen( szTmp ) );
  201.  
  202.                            wsprintf(szTmp, "Button rect: left=%d"\
  203.                                            " top=%d right=%d bottom=%d", 
  204.                                     rect.left, rect.top, rect.right, rect.bottom );
  205.                            TextOut( hdc, 10, 80, szTmp, strlen( szTmp ) );
  206.  
  207.                            ReleaseDC( hWnd, hdc );
  208.                         }
  209.                         break;
  210.  
  211.                  case IDM_ABOUT :
  212.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  213.                         break;
  214.  
  215.                  case IDM_EXIT :
  216.                         DestroyWindow( hWnd );
  217.                         break;
  218.               }
  219.               break;
  220.       
  221.       case WM_DESTROY :
  222.               PostQuitMessage(0);
  223.               break;
  224.  
  225.       default :
  226.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  227.    }
  228.  
  229.    return( 0L );
  230. }
  231.  
  232.  
  233. LRESULT CALLBACK About( HWND hDlg,           
  234.                         UINT message,        
  235.                         WPARAM wParam,       
  236.                         LPARAM lParam)
  237. {
  238.    switch (message) 
  239.    {
  240.        case WM_INITDIALOG: 
  241.                return (TRUE);
  242.  
  243.        case WM_COMMAND:                              
  244.                if (   LOWORD(wParam) == IDOK         
  245.                    || LOWORD(wParam) == IDCANCEL)    
  246.                {
  247.                        EndDialog(hDlg, TRUE);        
  248.                        return (TRUE);
  249.                }
  250.                break;
  251.    }
  252.  
  253.    return (FALSE); 
  254. }
  255.